package com.cl.mongo;
/*

                  写字楼里写字间，写字间里程序员；
                  程序人'猿'写程序，又拿程序换酒钱。
                  酒醒只在网上坐，酒醉还来网下眠；
                  酒醉酒醒日复日，网上网下年复年。
                  但愿老死电脑间，不愿鞠躬老板前；
                  奔驰宝马贵者趣，公交自行程序员。
                  别人笑我忒疯癫，我笑自己命太贱；
                  不见满街漂亮妹，哪个归得程序'猿'？
━━━━━━神兽出没━━━━━━
 ┏┓   ┏┓
┏┛┻━━━┛┻┓
┃       ┃
┃   ━   ┃
┃ ┳┛ ┗┳ ┃
┃       ┃
┃   ┻   ┃
┃       ┃
┗━┓   ┏━┛
  ┃   ┃                    神兽保佑, 永无BUG!
  ┃   ┃       Code is far away from bug with the animal protecting
  ┃   ┗━━━┓
  ┃       ┣┓
  ┃       ┏┛
  ┗┓┓┏━┳┓┏┛
   ┃┫┫ ┃┫┫
   ┗┻┛ ┗┻┛
 */

import com.cl.mongo.pb.UserVo;
import com.google.protobuf.InvalidProtocolBufferException;
import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.DeleteResult;
import org.bson.Document;
import org.bson.types.Binary;

import java.util.function.Consumer;

/**
 * Created by 石头哥哥 on 1/8/16.
 * <p>
 * mongo_test
 * <p>
 * com.cl.mongo
 * <p>
 * comments:
 */
public class TestMongondb {

    /**
     * @param args
     */
    public static void main(String[] args) {
        MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);
        MongoDatabase database = mongoClient.getDatabase("test0");

        UserVo.Builder builder = UserVo.newBuilder();
        builder.setUsername("石头哥哥");

        Document document = new Document();
        //binary
        document.append("user", builder.build().toByteArray())
                .append("value", "石头哥哥");


        MongoCollection<Document> documentMongoCollection = database.getCollection("t_user");


        //find
        FindIterable<Document> documents = documentMongoCollection.find();
        documents.forEach(new Consumer<Document>() {
            @Override
            public void accept(Document document) {
                byte[] data = ((Binary) document.get("user")).getData();
                try {
                    UserVo userVo = UserVo.parseFrom(data);
                    System.out.println(document.getObjectId("_id").getDate());
                    System.out.println(userVo.toString());
                } catch (InvalidProtocolBufferException e) {
                    e.printStackTrace();
                }

            }
        });

        FindIterable<Document> iterable_user0 = documentMongoCollection.find(Filters.or(new Document("value", "石头哥哥"), Filters.gt("value", "石头哥哥")));
        iterable_user0.forEach(new Consumer<Document>() {
            @Override
            public void accept(Document document) {
                byte[] data = ((Binary) document.get("user")).getData();
                try {
                    UserVo userVo = UserVo.parseFrom(data);
                    System.out.println(document.getObjectId("_id").getDate());
                    System.out.println(userVo.toString());
                    System.out.println(document.getString("value"));
                } catch (InvalidProtocolBufferException e) {
                    e.printStackTrace();
                }

            }
        });


        //update
        documentMongoCollection.findOneAndUpdate(new Document("value", "石头哥哥"), new Document("$set", new Document("value", "abcdefg")));
        iterable_user0 = documentMongoCollection.find(Filters.or(new Document("value", "abcdefg"), Filters.gt("value", "abcdefg")));
        iterable_user0.forEach(new Consumer<Document>() {
            @Override
            public void accept(Document document) {
                byte[] data = ((Binary) document.get("user")).getData();
                try {
                    UserVo userVo = UserVo.parseFrom(data);
                    System.out.println(document.getObjectId("_id").getDate());
                    System.out.println(userVo.toString());
                    System.out.println(document.getString("value"));
                } catch (InvalidProtocolBufferException e) {
                    e.printStackTrace();
                }
            }
        });



        //insert
        documentMongoCollection.insertOne(document);

        //creaateIndex
//        documentMongoCollection.createIndex();


        //true if a new document should be inserted if there are no matches to the query filter
        documentMongoCollection.updateOne(document, new Document("$set", new Document("value", "xxxxx")));

        documentMongoCollection.find(new Document("value", "xxxxx")).forEach(new Block<Document>() {
            @Override
            public void apply(Document document) {
                System.out.println("find document....");
                System.out.println(document.getString("value"));
            }
        });



        //delete
        DeleteResult deleteResult = documentMongoCollection.deleteMany(new Document());
        System.out.println(deleteResult.getDeletedCount());


    }
}
